
export function monitorScroll(){
    let last = 0, timer = null;
    function throttle() {
        let now = + new Date()
        if(now - last > 1000 ){
            last = now
            monitorScroll2()
        }
    }

    let developBody = this.$parent.$refs,
        scrollArr = [],
        that = this
    for (const key in developBody) {
        // console.log(developBody[key])
        if(developBody[key].$el.id == 'nav-flag'){
            // console.log('nav-flag')
        }else{
            
            let el = developBody[key].$el,
                height =  getElementTop(el) - 170
            let json = {
                scrollMaxHeight: el.clientHeight + height,
                scrollHeight: height
            }
            scrollArr.push(json)
        }
        
    }
    function getElementTop(element){
        // console.log(element)
　　　　var actualTop = element.offsetTop;
　　　　var current = element.offsetParent;

　　　　if (current !== null){
　　　　　　actualTop += current.offsetTop;
　　　　　　current = current.offsetParent;
　　　　}

　　　　return actualTop;
　　}

    function monitorScroll2(){
        console.log(+new Date())
        let wtop = document.documentElement.scrollTop||document.body.scrollTop;
        // console.log(scrollArr)
        scrollArr.forEach((item, index) => {
            if(wtop>item.scrollHeight && wtop < item.scrollMaxHeight){
                that.$set(that, 'onItem', index)
            }
        })

    }
    function addEvent( obj , type , eFn ) {
        function fn(e , b){
            e = e || window.event;
            eFn.call( obj , e  ) //处理this指向跟event问题
        }
        if( window.addEventListener ){
            obj.addEventListener( type , fn ,false )
        } else {
            obj.attachEvent( 'on' + type , fn )
        }
        return fn; //把上面的fn函数return上面 上面用一个变量来接收  为了清除事件
    }
    addEvent(window, 'scroll', throttle)

    
}